package com.xiaochong.bitong.api.service;

import com.alibaba.fastjson.JSON;
import com.xc.logclient.utils.LogTrace;
import com.xiaochong.bitong.api.config.H5Url;
import com.xiaochong.bitong.api.constant.ReturnCode;
import com.xiaochong.bitong.api.utils.ListSortUtil;
import com.xiaochong.bitong.api.utils.StringUtil;
import com.xiaochong.service.common.config.oss.OSSTool;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.data.mapper.CurrencyInfoMapper;
import com.xiaochong.tir.common.data.mapper.SyChannelsMapper;
import com.xiaochong.tir.common.data.mapper.SyResearchReportFileMapper;
import com.xiaochong.tir.common.data.mapper.SyResearchReportMapper;
import com.xiaochong.tir.common.data.po.CurrencyInfo;
import com.xiaochong.tir.common.data.po.SyChannels;
import com.xiaochong.tir.common.data.po.SyResearchReport;
import com.xiaochong.tir.common.data.po.SyResearchReportFile;
import com.xiaochong.tir.common.data.vo.ApiResultVo;
import com.xiaochong.tir.common.data.vo.CurrencyQuotationResultVo;
import com.xiaochong.tir.common.data.vo.CurrencyQuotationVo;
import com.xiaochong.tir.common.util.ApiResultCodeUtils;
import com.xiaochong.tir.common.util.OssImageUtil;
import com.xiaochong.tir.common.util.RedisKeysUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Semaphore;

/**
 * 此类为[基础代码]自动生成,已经拥有基本的增删改成操作 
 * <其他请自行扩展>
 * @author mengxiangdong
 * @time 2018-05-23 14:07
 *
 */
@Service
public class ReportService {

    @Autowired
    private SyResearchReportMapper syResearchReportMapper;

    @Autowired
    private SyResearchReportFileMapper syResearchReportFileMapper;

    @Autowired
    private SyChannelsMapper syChannelsMapper;

    @Autowired
    private OSSTool ossTool;

    @Autowired
    OssImageUtil ossImageUtil;

    /**
     *
     研报列表
     * @return List<T>
     */
    public ApiResultVo reportList(CurrencyQuotationVo currencyQuotationVo){
        ApiResultVo resultVo=new ApiResultVo();
        try {

            SyResearchReport syResearchReport=new SyResearchReport();
            syResearchReport.setEnableFlag(true);
            syResearchReport.setDeleteFlag(false);
            syResearchReport.setPublishPlatform( "2" );
            List<SyResearchReport> list=syResearchReportMapper.getReportAllList(syResearchReport);

            for (int i=0;i<list.size();i++){
                List<String> strList=new ArrayList<>();
                SyResearchReport syRe=new SyResearchReport();
                syRe.setId(list.get(i).getId());
                List<SyResearchReport> syList=syResearchReportMapper.getReportList(syRe);
                for (int j = 0; j < syList.size(); j++) {
                    SyResearchReport report = syList.get(j);
                    if (report!=null){
                        strList.add(syList.get(j).getChannelsName());
                    }
                }
                List<String> imgList=new ArrayList<>();
                SyResearchReportFile syResearchReportFile=new SyResearchReportFile();
                syResearchReportFile.setResearchReportId(list.get(i).getId());
                syResearchReportFile.setType(0);
                syResearchReportFile.setDeleteFlag( false );
                syResearchReportFile.setPublishPlatform( "2" );
                List<SyResearchReportFile> fileList=syResearchReportFileMapper.selectByEntityList(syResearchReportFile);
                if(fileList != null && fileList.size() > 0){
                    list.get(i).setAppImgUrl( ossImageUtil.fmtImg( fileList.get(0).getFileName() ));
                }

                SyChannels syChannels=new SyChannels();
                List<String> chalStrList=new ArrayList<>();
                syChannels.setId( list.get( i ).getChannelsId() );
                syChannels.setEnableFlag( true );
                syChannels.setDeleteFlag( false );
                List<SyChannels> challList=syChannelsMapper.selectByEntityList(syChannels);
                for (int j=0;j<challList.size();j++){
                    chalStrList.add(  challList.get( j ).getName());
                }

                list.get( i ).setPdList( chalStrList );
                list.get(i).setChannelsNames(strList);
                list.get(i).setUrl(H5Url.getReport()+list.get( i ).getId());
            }

            //分页
            if(currencyQuotationVo.getPageIndex()==null){
                currencyQuotationVo.setPageIndex( 1 );
            }
            if(currencyQuotationVo.getPageSize()==null){
                currencyQuotationVo.setPageSize( 15 );
            }
            Object result= StringUtil.pageBean(list,currencyQuotationVo.getPageIndex(),currencyQuotationVo.getPageSize()  );

            resultVo.setCode( ReturnCode.SUCCESS);
            resultVo.setTradeId( LogTrace.getTrace().getTraceId() );
            resultVo.setData(list);
            resultVo.setTotal( list.size() );
            return resultVo;
        }catch (Exception e){
            e.printStackTrace();
            LogTrace.error("研报列表",e);
            resultVo.setCode(ReturnCode.ERROR);
            resultVo.setTradeId( LogTrace.getTrace().getTraceId() );
            return  resultVo;
        }
    }

    /**
     *
     研报详情
     * @return List<T>
     */
    public ApiResultVo reportDetails(SyResearchReport syResearchReport){
        ApiResultVo resultVo=new ApiResultVo();
        try {
            List<SyResearchReport> list=syResearchReportMapper.selectByEntityList(syResearchReport);

            if (list.size()>0){
                SyResearchReportFile file=new SyResearchReportFile();
                file.setType( 1 );
                file.setResearchReportId( list.get( 0 ).getId() );
                file.setPublishPlatform( "2" );
                List<SyResearchReportFile> fileList= syResearchReportFileMapper.selectByEntityList(file);

                List<String> strList=new ArrayList<>(  );

                for (int i=0;i<fileList.size();i++){
                    String filePath = fileList.get( i ).getFileName();
                    if(StringUtils.isNotBlank( filePath )&&filePath.contains( "http" )){
                        strList.add( ossImageUtil.fmtImg( fileList.get( i ).getFileName()));
                    }else{
                        String imgUrl=ossImageUtil.fmtImg( filePath );
                        strList.add( imgUrl );
                    }
                }

                for (int i=0;i<list.size();i++){
                    List<String> strList2=new ArrayList<>();
                    SyResearchReport syRe=new SyResearchReport();
                    syRe.setId(list.get(i).getId());
                    List<SyResearchReport> syList=syResearchReportMapper.getReportList(syRe);
                    for (int j = 0; j < syList.size(); j++) {
                        SyResearchReport report = syList.get(j);
                        if (report!=null){
                            strList2.add(syList.get(j).getChannelsName());
                        }
                    }
                    List<String> imgList=new ArrayList<>();
                    SyResearchReportFile syResearchReportFile=new SyResearchReportFile();
                    syResearchReportFile.setResearchReportId(list.get(i).getId());
                    syResearchReportFile.setType(0);
                    syResearchReportFile.setDeleteFlag( false );
                    syResearchReportFile.setPublishPlatform( "2" );
                    List<SyResearchReportFile> fileList2=syResearchReportFileMapper.selectByEntityList(syResearchReportFile);
                    if(fileList != null && fileList2.size() > 0){
                        list.get(i).setAppImgUrl(ossImageUtil.fmtImg( fileList2.get(0).getFilePath()));
                    }
                }

                list.get(0).setFilePath( strList );
                //浏览次数+1
                syResearchReportMapper.updateTourCount(syResearchReport);

                SyChannels syChannels=new SyChannels();
                List<String> chalStrList=new ArrayList<>();
                syChannels.setId( list.get( 0 ).getId() );
                syChannels.setEnableFlag( true );
                syChannels.setDeleteFlag( false );
                List<SyChannels> challList=syChannelsMapper.selectByEntityList(syChannels);
                for (int j=0;j<challList.size();j++){
                    chalStrList.add(  challList.get( j ).getName());
                }
                list.get( 0 ).setContent( list.get( 0 ).getContent().replace("OSSAccessKeyId" , "" ) );
                list.get(0).setPdList( chalStrList );
                resultVo.setCode(ReturnCode.SUCCESS);
                resultVo.setTradeId(LogTrace.getTrace().getTraceId());
                resultVo.setData(list.get(0));
            }else {
                resultVo.setCode(ReturnCode.DATA_NOT_EXIST);
                resultVo.setTradeId(LogTrace.getTrace().getTraceId());
            }
            return resultVo;
        }catch (Exception e){
            e.printStackTrace();
            LogTrace.error("研报详情",e);
            resultVo.setCode(ReturnCode.ERROR);
            resultVo.setTradeId(LogTrace.getTrace().getTraceId());
            return  resultVo;
        }
    }



}